package com.karaoke.android.kege.fragment


import android.os.Bundle
import android.support.v4.app.Fragment
import android.transition.Explode
import android.transition.Transition
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.karaoke.android.kege.ResetHwBoardEvent
import com.karaoke.android.kege.databinding.FragmentHandWriteBinding
import com.karaoke.android.kege.vm.SearchViewModel
import org.simple.eventbus.EventBus
import org.simple.eventbus.Subscriber
import universum.studios.android.fragment.BaseFragment
import kotlin.properties.Delegates

/**
 * A simple [Fragment] subclass.
 */
class HandWriteFragment : BaseFragment()
{
    companion object
    {
        fun newInstance(model: SearchViewModel) = HandWriteFragment().apply {
            this.model = model
        }
    }

    override fun getEnterTransition(): Transition = Explode().apply { duration = 200 }

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onDestroy()
    {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }


    private var mBinding: FragmentHandWriteBinding? = null


    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        if (mBinding == null)
        {
            mBinding = FragmentHandWriteBinding.inflate(inflater, container, false)
            mBinding?.viewModel = model
        }
        return mBinding?.root
    }

    @Subscriber
    fun cleanBoard(e: ResetHwBoardEvent)
    {
        mBinding?.paintView?.resetRecognize()
    }

    private var model: SearchViewModel by Delegates.notNull()

}
